package org.softmed.rest.server.control;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ConnectException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.restlet.Client;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Response;
import org.restlet.resource.StringRepresentation;
import org.softmed.rest.editor.commons.SwingUtil;
import org.softmed.rest.editor.commons.URIProvider;

public class ControlPane extends JPanel implements ActionListener {

	String refreshURI = "/admin/refresh";
	String refreshAOPURI = "/admin/refresh-aop";
	String refreshRESTURI = "/admin/refresh-rest";
	String rootURIURI = "/admin/root-uri";

	Client client = new Client(Protocol.HTTP);

	URIProvider uriProvider;

	JPanel left = new JPanel();
	JPanel right = new JPanel();

	JButton simple = new JButton("Simple Refresh");
	JButton allButAdmin = new JButton("Delete all databases except Admin");
	JButton backup = new JButton("Backup databases");
	JButton all = new JButton("Delete all databases");
	JButton setDefault = new JButton(
			"Delete all databases & set default Admin databases");
	JButton deleteSingleProvider = new JButton(
			"Delete Single Persistence Provider");
	JTextField provider = new JTextField();

	JButton refreshAOP = new JButton("Refresh AOP");
	JButton refreshREST = new JButton("Refresh stored REST Services");

	JButton getRootURI = new JButton("Get Root URI");
	JButton setRootURI = new JButton("Set Root URI");
	JTextField rootURI = new JTextField();

	int leftWidth = 340;
	int leftHeight = 20;

	int rightWidth = 230;
	int rightHeight = 20;

	int totalHeight = 140;

	ControlPane() {
		SwingUtil.setSize(this, leftWidth + rightHeight, totalHeight);
		SwingUtil.setSize(left, leftWidth, totalHeight);
		SwingUtil.setSize(right, rightWidth, totalHeight);

		// left.setBackground(Color.RED);
		// right.setBackground(Color.GREEN);

		// left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
		left.setLayout(new GridLayout(7, 1, 0, 0));
		right.setLayout(new GridLayout(7, 1, 0, 0));

		SwingUtil.setSize(allButAdmin, leftWidth, leftHeight);
		SwingUtil.setSize(simple, leftWidth, leftHeight);
		SwingUtil.setSize(backup, leftWidth, leftHeight);
		SwingUtil.setSize(all, leftWidth, leftHeight);
		SwingUtil.setSize(setDefault, leftWidth, leftHeight);
		SwingUtil.setSize(deleteSingleProvider, leftWidth, leftHeight);
		SwingUtil.setSize(all, leftWidth, leftHeight);
		SwingUtil.setSize(provider, leftWidth - 86, leftHeight);

		SwingUtil.setSize(refreshAOP, rightWidth, rightHeight);
		SwingUtil.setSize(refreshREST, rightWidth, rightHeight);
		SwingUtil.setSize(getRootURI, rightWidth, rightHeight);
		SwingUtil.setSize(setRootURI, rightWidth, rightHeight);
		SwingUtil.setSize(rootURI, rightWidth - 50, rightHeight);

		JPanel rootURIPanel = new JPanel();
		rootURIPanel
				.setLayout(new BoxLayout(rootURIPanel, BoxLayout.LINE_AXIS));
		rootURIPanel.add(new JLabel("Root URI:"));
		rootURIPanel.add(rootURI);

		JPanel providerPanel = new JPanel();
		providerPanel.setLayout(new BoxLayout(providerPanel,
				BoxLayout.LINE_AXIS));
		providerPanel.add(new JLabel("Provider Name:"));
		providerPanel.add(provider);

		left.add(allButAdmin);
		left.add(simple);
		left.add(backup);
		left.add(setDefault);
		left.add(all);
		left.add(deleteSingleProvider);
		left.add(providerPanel);

		right.add(refreshAOP);
		right.add(refreshREST);
		right.add(getRootURI);
		right.add(setRootURI);
		right.add(rootURIPanel);

		simple.addActionListener(this);
		allButAdmin.addActionListener(this);
		backup.addActionListener(this);
		all.addActionListener(this);
		setDefault.addActionListener(this);
		deleteSingleProvider.addActionListener(this);
		refreshAOP.addActionListener(this);
		refreshREST.addActionListener(this);
		getRootURI.addActionListener(this);
		setRootURI.addActionListener(this);

		// SwingUtil.setSize(uri, righWidth, righHeight);

		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		add(left);
		add(right);

	}

	public URIProvider getUriProvider() {
		return uriProvider;
	}

	public void setUriProvider(URIProvider uriProvider) {
		this.uriProvider = uriProvider;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();

		String serverURI = uriProvider.getURI();
		if (serverURI == null || serverURI.length() == 0) {
			JOptionPane.showMessageDialog(this,
					"Please indicate a server uri to send requests to",
					"Error", JOptionPane.ERROR_MESSAGE);
			return;
		}

		Response response = null;
		try {

			if (source == simple) {
				response = client.get(serverURI + refreshURI);
				// return;
			} else if (source == backup) {
				response = client
						.post(serverURI + refreshURI, new StringRepresentation(
								"backup", MediaType.TEXT_PLAIN));
			} else if (source == allButAdmin) {
				response = client.post(serverURI + refreshURI,
						new StringRepresentation("delete-all-except-admin",
								MediaType.TEXT_PLAIN));
				// return;
			} else if (source == all) {
				response = client.post(serverURI + refreshURI,
						new StringRepresentation("delete-all",
								MediaType.TEXT_PLAIN));
				// return;
			} else if (source == setDefault) {
				response = client.post(serverURI + refreshURI,
						new StringRepresentation("default",
								MediaType.TEXT_PLAIN));
				// return;
			} else if (source == deleteSingleProvider) {
				response = client.post(serverURI + refreshURI,
						new StringRepresentation(provider.getText(),
								MediaType.TEXT_PLAIN));
				// return;
			} else if (source == refreshAOP) {
				response = client.get(serverURI + refreshAOPURI);
				// return;
			}

			if (source == refreshREST) {
				response = client.get(serverURI + refreshRESTURI);
				// return;
			} else if (source == getRootURI) {
				response = client.get(serverURI + rootURIURI);
				String rootURI = null;
				try {
					rootURI = response.getEntity().getText();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				this.rootURI.setText(rootURI);
				// return;
			} else if (source == setRootURI) {
				response = client.put(serverURI + rootURIURI,
						new StringRepresentation(this.rootURI.getText(),
								MediaType.TEXT_PLAIN));
				// return;
			}

			if (response != null && response.getStatus().getCode() == 1001)
				JOptionPane.showMessageDialog(this, response.getStatus()
						.getDescription(), response.getStatus().getName(),
						JOptionPane.ERROR_MESSAGE);

		} catch (Throwable t) {
			if (t instanceof ConnectException) {
				JOptionPane.showMessageDialog(this, t.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);

			} else {
				JOptionPane.showMessageDialog(this, "Connection refused.",
						"Connection Error", JOptionPane.ERROR_MESSAGE);

			}
		}

		System.out.println("Request Done !!!");

	}

}
